﻿var allowEx = [".jpg", ".png", ".bmp",".gif"];
var rootPath = getPath(fl.scriptURI);
ergodicFolder(rootPath);
function ergodicFolder(path)
{
	var files = FLfile.listFolder(path, "files");
	//循环列表，在当前目录创建同名的flash文件
	var i;
	for(i = 0; i < files.length; i++)
	{
		if(allowEx.indexOf(getExtension(files[i]).toLowerCase()) == -1)
		{
			files.splice(i, 1);
			i--;
		}
	}

	for(var i = 0; i < files.length; i++)
	{
		fl.trace(files[i]);
		if(allowEx.indexOf(getExtension(files[i]).toLowerCase()) != -1)
		{
			fl.trace("第" + (i + 1) + "个文件，还剩" + (files.length - 1 - i));
			var flName = path + getFileNameWithoutExtension(files[i]);
			fl.trace(flName);
			if(FLfile.exists(flName))
				FLfile.remove(flName);
			var document = fl.createDocument("timeline");
			document.importFile(path + files[i], false);
			document.selectAll();
			var element = document.selection[0];
			element.x = 0;
			element.y = 0;
			document.width = parseInt(element.width);
			document.height = parseInt(element.height);
			//fl.saveDocument(document, flName + ".fla");
			for(var j = 0; j < document.library.items.length; j++)
			{
				if(document.library.items[j].itemType == "bitmap")
				{
					document.library.items[j].compressionType = "photo"; 
					document.library.items[j].useImportedJPEGQuality = true; 
					document.library.items[j].quality = 90;
				}
			}
			document.exportSWF(flName + ".swf");
			document.close(false);
		}
	}
	fl.trace("完成");
}

 /**
  * 获取去掉文件名的路径
  */
function getPath(path)
{
	var index = Math.max(path.lastIndexOf("\\"), path.lastIndexOf("/"));
	return path.substring(0, index + 1);
}
/**
 * 获取文件名
 */
function getFileName(path)
{
	var index = Math.max(path.lastIndexOf("\\"), path.lastIndexOf("/"));
	index += 1;
	return path.substring(index, path.length);
}

/**
 * 获取扩展名
 */
function getExtension(path)
{
	return path.substring(path.lastIndexOf("."), path.length);
}

/**
 * 获取不带扩展名的文件名
 */
function getFileNameWithoutExtension(path)
{
	var index = Math.max(path.lastIndexOf("\\"), path.lastIndexOf("/"));
	index += 1;
	return path.substring(index, path.lastIndexOf("."));
}